package cn.edu.csust.coursecard.service.impl;

import cn.edu.csust.coursecard.dao.dto.IndexPost;
import cn.edu.csust.coursecard.dao.entity.PostReport;
import cn.edu.csust.coursecard.dao.entity.PostSearchVo;
import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.common.RedisKeyEnum;
import cn.edu.csust.coursecard.dao.PostMapper;
import cn.edu.csust.coursecard.dao.PostReportMapper;
import cn.edu.csust.coursecard.dao.dto.PostReportDetail;
import cn.edu.csust.coursecard.exception.BaseException;
import cn.edu.csust.coursecard.service.PostReportService;
import cn.edu.csust.coursecard.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class PostReportServiceImpl implements PostReportService {
    @Autowired
    PostReportMapper postReportMapper;
    @Autowired
    PostMapper postMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public void addPostReport(Integer userId,Integer postId) {
        String postReportId = (String) redisUtil.get(RedisKeyEnum.POST_REPORT_ID+userId+postId);
        if(!StringUtils.isEmpty(postReportId)){
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "你已举报！");
        }
        PostReport postReport = postReportMapper.selectByUserIdAndPostId(userId,postId);
        if(!Objects.isNull(postReport)){
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "你已举报！");
        }
        postReport = new PostReport();
        postReport.setUserId(userId);
        postReport.setPostId(postId);
        postReport.setCreateTime(new Date());
        postReport.setUpdateTime(new Date());
        postReport.setIsValid((byte) 1);
        postReportMapper.insertSelective(postReport);
        redisUtil.set(RedisKeyEnum.POST_REPORT_ID+userId+userId,String.valueOf(postReport.getId()),RedisKeyEnum.TOKEN_EXPIRE);
    }

    @Override
    public List<PostReportDetail> selectPostReportDetail() {
        List<Integer> postIds = postReportMapper.selectPostIds();
//        System.out.println(postIds);
        Map<Integer,Integer> postReportNum = new HashMap<>();
        postIds.forEach(postId -> {
            if(postReportNum.containsKey(postId)){
                Integer num = postReportNum.get(postId);
                postReportNum.put(postId,++num);
            }else {
                postReportNum.put(postId,1);
            }
        });
        List<PostReportDetail> postReportDetails = new ArrayList<>();
        Set<Integer> integers = postReportNum.keySet();
        integers.forEach(postId -> {
            PostSearchVo postSearchVo = new PostSearchVo();
            postSearchVo.setPostId(postId);
            List<IndexPost> list = postMapper.selectIndexPostByPostVo(postSearchVo);
            if(list.size()!=0){
                postReportDetails.add(new PostReportDetail(postMapper.selectIndexPostByPostVo(postSearchVo).get(0),postReportNum.get(postId)));
            }
        });
        postReportDetails.sort((o1, o2) -> o2.getReportNum()-o1.getReportNum());
        return postReportDetails;
    }

    @Override
    public void deletePostReport(Integer userId, Integer postId) {
        if(userId == 0){
            postReportMapper.deleteByPostId(postId);
            return;
        }
        int row = postReportMapper.deleteByUserIdAndPostId(userId,postId);
        if(row!=1){
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "取消举报失败！");
        }
        redisUtil.remove(RedisKeyEnum.POST_REPORT_ID+userId+postId);
    }

}
